package com.interview.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.interview.controller.entity.Region;
import netscape.javascript.JSObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class InterviewController {
    private List<Region> regionDataList;

    @PostConstruct
    public void  init(){
        regionDataList = new ArrayList<>();
        Region  region = new Region(1,"中国",null);
        Region  region2 = new Region(2,"浙江省",1);
        Region  region8 = new Region(8,"湖北省",1);
        Region  region3 = new Region(3,"杭州市",2);
        Region  region4 = new Region(4,"余杭区",3);
        Region  region5 = new Region(5,"滨江区",3);
        Region  region6 = new Region(6,"上城区",3);
        Region  region7 = new Region(7,"西湖区",3);
        regionDataList.add(region);
        regionDataList.add(region2);
        regionDataList.add(region3);
        regionDataList.add(region4);
        regionDataList.add(region5);
        regionDataList.add(region6);
        regionDataList.add(region7);
        regionDataList.add(region7);
        regionDataList.add(region8);
    }



    @GetMapping("/getRegionListData")
    public Object getRegionListData(Integer regionId){
        Region region = getRegionById(regionId,regionDataList);
        JSONObject result = new JSONObject();
        if (region == null){
            return  result;
        }
        handData(result,region);
        return  result;
    }


    public void handData(JSONObject jsonObject, Region region){
        String regionName = region.getRegionName();
        Integer regionId = region.getRegionId();
        List<Region> regionByParentRegionIdList = getRegionByParentRegionId(regionId,regionDataList);
        if (regionByParentRegionIdList.size() ==0){
            return;
        }
        jsonObject.put(regionName,new JSONObject());
        for (Region r : regionByParentRegionIdList) {
            jsonObject.getJSONObject(regionName).put(r.getRegionName(),new JSONObject());
            handData(jsonObject.getJSONObject(regionName),r);
        }
    }

    /**这里 模拟根据id查询行政区**/
    public Region getRegionById(Integer regionId,List<Region> regionDataList){

        for (Region region : regionDataList) {
            if (region.getRegionId().equals(regionId)){
                return region;
            }
        }
        return  null;
    }

    /**这里 模拟根据父级区域id查询行政区**/
    public  List<Region> getRegionByParentRegionId(Integer parentRegionId,List<Region> regionDataList){
        List<Region> regionList = new ArrayList<>();
        for (Region region : regionDataList) {
            if (region.getParentRegionId()!=null && region.getParentRegionId().equals(parentRegionId)){
                regionList.add(region);
            }
        }
        return  regionList;
    }

}
